package cn.edu.jju.stepcounter2.chart.presenter;


import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import cn.edu.jju.stepcounter2.R;
import cn.edu.jju.stepcounter2.bean.DayData;
import cn.edu.jju.stepcounter2.chart.model.ChartModel;
import cn.edu.jju.stepcounter2.chart.model.ChartModelImpl;
import cn.edu.jju.stepcounter2.chart.view.ChartView;
import cn.edu.jju.stepcounter2.test.TestActivity;
import cn.edu.jju.stepcounter2.utils.GetDate;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.listener.ColumnChartOnValueSelectListener;
import lecho.lib.hellocharts.listener.ViewportChangeListener;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.ColumnChartView;
import lecho.lib.hellocharts.view.PreviewColumnChartView;
import rx.Scheduler;
import rx.Single;
import rx.SingleSubscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static android.util.Log.i;

/**
 * Created by roor on 2016/12/15
 */

public class ChartPresenterImpl implements ChartPresenter {
    private ChartView chartView;
    private ChartModel chartModel;

    private ColumnChartView chart;
    private PreviewColumnChartView previewChart;

    private ColumnChartData data;
    private ColumnChartData previewData;
    private List<DayData> dayDataList;

    public ChartPresenterImpl(ChartView chartView) {
        this.chartModel = new ChartModelImpl();
        this.chartView = chartView;
        chart = chartView.getChartView();
        previewChart = chartView.getPreviewView();
    }

    @Override
    public void showChart() {
        Single<List<DayData>> single = chartModel.getAllDayData();
        Subscription sub = single.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SingleSubscriber<List<DayData>>() {
                    @Override
                    public void onSuccess(List<DayData> value) {
                        generateDefaultData(value);
                        i("@@@@@", "showchart123" + value.size());

                    }

                    @Override
                    public void onError(Throwable error) {


                    }
                });








    }

    private void generateDefaultData(List<DayData> dayDatas) {
        dayDataList = dayDatas;
        int numSubcolumns = 1;
        int numColumns = dayDatas.size();
        List<Column> columns = new ArrayList<Column>();
        List<SubcolumnValue> values;
        List<AxisValue> axisValuesX = new ArrayList<>();
        for (int i = 0; i < numColumns; ++i) {
            axisValuesX.add(new AxisValue(i).setLabel(dayDatas.get(i).getDate()));
            values = new ArrayList<SubcolumnValue>();
            for (int j = 0; j < numSubcolumns; ++j) {
                i("@@@@@", "generateDefaultData" + dayDatas.get(i).getSteps());
                values.add(new SubcolumnValue(Integer.parseInt(dayDatas.get(i).getSteps()), ChartUtils.pickColor()));

            }

            columns.add(new Column(values));

        }

        data = new ColumnChartData(columns);


        data.setAxisXBottom(new Axis(axisValuesX));
        data.setAxisYLeft(new Axis().setHasLines(true));

        // prepare preview data, is better to use separate deep copy for preview chart.
        // set color to grey to make preview area more visible.
        previewData = new ColumnChartData(data);
        for (Column column : previewData.getColumns()) {
            for (SubcolumnValue value : column.getValues()) {
                value.setColor(ChartUtils.DEFAULT_DARKEN_COLOR);
            }
        }
        chart.setColumnChartData(data);
        // Disable zoom/scroll for previewed chart, visible chart ranges depends on preview chart viewport so
        // zoom/scroll is unnecessary.
        chart.setZoomEnabled(false);
        chart.setScrollEnabled(false);
        chart.setOnValueTouchListener(new ColumnSelectListener());

        previewChart.setColumnChartData(previewData);
        previewChart.setViewportChangeListener(new ViewportListener());
        Viewport tempViewport = new Viewport(chart.getMaximumViewport());
        float dx = tempViewport.width() / 4;
        tempViewport.inset(dx, 0);

        previewChart.setCurrentViewportWithAnimation(tempViewport);

        previewChart.setZoomType(ZoomType.HORIZONTAL);

    }
    private class ColumnSelectListener implements ColumnChartOnValueSelectListener{

        @Override
        public void onValueSelected(int columnIndex, int subcolumnIndex, SubcolumnValue value) {
            DayData data = dayDataList.get(columnIndex);
            if (data.getDate().equals(GetDate.get())) {
                 chartView.getTextViewById(R.id.tv_date).setText("今天");
            }else {
                chartView.getTextViewById(R.id.tv_date).setText(data.getDate());
            }
            DecimalFormat decimalFormat = new DecimalFormat("0.00");

            String calConsume = decimalFormat.format(Double.parseDouble(data.getCalConsume())/1000);
            chartView.getTextViewById(R.id.tv_calConsume).setText(calConsume);
            chartView.getTextViewById(R.id.tv_steps).setText(data.getSteps());
            chartView.getTextViewById(R.id.tv_expectStep).setText(data.getExpectStep());
        }

        @Override
        public void onValueDeselected() {

        }
    }

    private class ViewportListener implements ViewportChangeListener {

        @Override
        public void onViewportChanged(Viewport newViewport) {
            // don't use animation, it is unnecessary when using preview chart because usually viewport changes
            // happens to often.
            chart.setCurrentViewport(newViewport);
        }

    }
}